package com.example.music;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.music.Home.HomeFragment;
import com.example.music.Mine.MineFragment;
import com.example.music.base.BaseActivity;
import com.example.music.base.CommonViewPagerAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.iv_home)
    ImageView iv_home;
    @BindView(R.id.iv_mine)
    ImageView iv_mine;

    private ArrayList<Fragment> mPagerFragment = new ArrayList<>();
    private CommonViewPagerAdapter mPagerAdapter;

    @OnClick({R.id.ll_home, R.id.ll_mine})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_home:
                //首页
                setPager(0);
                break;
            case R.id.ll_mine:
                //我的
                setPager(1);
                break;
        }
    }
    @Override
    protected int initView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        initView();
        initVP();
        initListener();  //设置监听

    }


    private void initListener() {
        //滑动监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //改颜色，菜单栏
                upPagerStyle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 切换页面
     *
     * @param position 选中的下标
     */
    private void setPager(int position) {
        //样式更改
        upPagerStyle(position);
        //切换选中的页面
        viewPager.setCurrentItem(position);
    }

    /**
     * 页面样式更改
     *
     * @param position
     */
    private void upPagerStyle(int position) {
        //样式全部改为"未选中"
        iv_home.setSelected(false);
        iv_mine.setSelected(false);
        //样式更改为"选中"
        if (position == 0) {
            iv_home.setSelected(true);
        } else if (position == 1) {
            iv_mine.setSelected(true);
        }
    }

    private void initView() {
        viewPager = findViewById(R.id.viewPager);
        iv_home = findViewById(R.id.iv_home);
        iv_mine = findViewById(R.id.iv_mine);
    }

    private void initVP() {
        viewPager.setOffscreenPageLimit(2);
        mPagerAdapter = new CommonViewPagerAdapter(getSupportFragmentManager(), mPagerFragment);
        viewPager.setAdapter(mPagerAdapter);

        mPagerFragment.add(HomeFragment.newInstance());
        mPagerFragment.add(MineFragment.newInstance());
        mPagerAdapter.notifyDataSetChanged();
        //默认选中第一个页面
        setPager(0);
    }


}